package com.book.system.servlet;

import com.alibaba.fastjson.JSON;
import com.book.system.commom.R;
import com.book.system.entity.BookInfo;
import com.book.system.service.BookService;
import com.book.system.service.impl.BookServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.print.Book;
import java.util.List;
import java.util.Map;

/**
 * @author ：oath
 * @date ：Created in 2024/4/27 22:42
 */
@WebServlet("/book")
public class BookServlet extends BaseServlet {
    /**
     * 创建业务逻辑层对象
     */
    private final BookService bookService = new BookServiceImpl();

    /**
     * 获取图书列表
     * @author : oath
     * @create time : 2024/4/27 23:06
     * @param request 请求对象
     * @param response 响应对象
     *
     * @return com.book.system.commom.R<java.util.List<java.util.Map<java.lang.String,java.lang.Object>>>
     */
    public R<List<Map<String, Object>>> getList(HttpServletRequest request, HttpServletResponse response) {

        // 获取前端传递的参数
        String name = request.getParameter("name");
        String classId = request.getParameter("classId");
        // 调用业务逻辑层方法
        List<Map<String, Object>> list = bookService.getList(name, Integer.valueOf(classId));
        return R.success(list);

    }

    /**
     * 添加图书
     * @author : oath
     * @create time : 2024/5/9 12:31
     * @param request 请求对象
     * @param response 响应对象
     * @return com.book.system.commom.R<java.lang.String>
     */
    public R<String>  add(HttpServletRequest request, HttpServletResponse response) {
        //获取请求体数据
        String requestBody = getRequestBody(request);
        //将JSON字符串转换成Java对象
        BookInfo book = JSON.parseObject(requestBody, BookInfo.class);
        bookService.add(book);
        return R.success("添加成功");
    }

    /**
     * 更新图书
     * @author : oath
     * @create time : 2024/5/9 12:32
     * @param request 请求对象
     * @param response 响应对象
     * @return com.book.system.commom.R<java.lang.String>
     */
    public R<String>  update(HttpServletRequest request, HttpServletResponse response) {
        //获取请求体数据
        String requestBody = getRequestBody(request);
        //将JSON字符串转换成Java对象
        BookInfo book = JSON.parseObject(requestBody, BookInfo.class);
        bookService.updateById(book);
        return R.success("修改成功");
    }
    /**
     * 删除图书
     * @author : oath
     * @create time : 2024/5/9 12:32
     * @param request 请求对象
     * @param response 响应对象
     * @return com.book.system.commom.R<java.lang.String>
     */
    public R<String>  delete(HttpServletRequest request, HttpServletResponse response) {
        //获取前端参数
        String id = request.getParameter("id");
        bookService.delete(Integer.valueOf(id));
        return R.success("删除成功");
    }



}